/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginView.java
 *
 * Created on Apr 29, 2011, 9:50:20 PM
 */
package edu.chalmers.se.tda367.gui.view;

import edu.chalmers.se.tda367.database.DatabaseController;
import edu.chalmers.se.tda367.gui.window.AddUserWindow;
import edu.chalmers.se.tda367.gui.window.AddTeacherWindow;
import edu.chalmers.se.tda367.gui.panel.UserListPanel;

import edu.chalmers.se.tda367.gui.panel.ImagePanel;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileNotFoundException;



import edu.chalmers.se.tda367.observer.GlobalObserverList;


import edu.chalmers.se.tda367.main.Pluggis;
import edu.chalmers.se.tda367.model.User;
import java.io.IOException;

/**
 *
 * @author pierrereimertz
 */
public class LoginView extends ImagePanel implements PropertyChangeListener {

    /** Creates new form LoginView */
    public LoginView() throws FileNotFoundException {
        initComponents();

        GlobalObserverList.getInstance().addObserver(this);
        jUserPanel.add(new UserListPanel());
        this.setImage("pictures/bg.jpg");
        this.validate();
        jScrollPane1.getViewport().setOpaque(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLoginPanel = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        nameField = new javax.swing.JTextField();
        loginButton = new javax.swing.JButton();
        jPasswordField = new javax.swing.JPasswordField();
        infoLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jUserPanel = new javax.swing.JPanel();
        AddUserButton = new javax.swing.JButton();
        addTeacherButton = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(820, 550));
        setMinimumSize(new java.awt.Dimension(820, 550));
        setPreferredSize(new java.awt.Dimension(820, 550));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getResourceMap(LoginView.class);
        jLoginPanel.setBackground(resourceMap.getColor("jLoginPanel.background")); // NOI18N
        jLoginPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jLoginPanel.setName("jLoginPanel"); // NOI18N

        nameLabel.setFont(resourceMap.getFont("nameLabel.font")); // NOI18N
        nameLabel.setText(resourceMap.getString("nameLabel.text")); // NOI18N
        nameLabel.setName("nameLabel"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        nameField.setEditable(false);
        nameField.setFont(resourceMap.getFont("nameField.font")); // NOI18N
        nameField.setText(resourceMap.getString("nameField.text")); // NOI18N
        nameField.setName("nameField"); // NOI18N

        loginButton.setFont(resourceMap.getFont("loginButton.font")); // NOI18N
        loginButton.setText(resourceMap.getString("loginButton.text")); // NOI18N
        loginButton.setName("loginButton"); // NOI18N
        loginButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginButtonActionPerformed(evt);
            }
        });

        jPasswordField.setFont(resourceMap.getFont("jPasswordField.font")); // NOI18N
        jPasswordField.setText(resourceMap.getString("jPasswordField.text")); // NOI18N
        jPasswordField.setName("jPasswordField"); // NOI18N

        infoLabel.setFont(resourceMap.getFont("infoLabel.font")); // NOI18N
        infoLabel.setForeground(resourceMap.getColor("infoLabel.foreground")); // NOI18N
        infoLabel.setText(resourceMap.getString("infoLabel.text")); // NOI18N
        infoLabel.setName("infoLabel"); // NOI18N

        org.jdesktop.layout.GroupLayout jLoginPanelLayout = new org.jdesktop.layout.GroupLayout(jLoginPanel);
        jLoginPanel.setLayout(jLoginPanelLayout);
        jLoginPanelLayout.setHorizontalGroup(
            jLoginPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLoginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(jLoginPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLoginPanelLayout.createSequentialGroup()
                        .add(loginButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 88, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(infoLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(jLoginPanelLayout.createSequentialGroup()
                        .add(jLoginPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(nameLabel))
                        .add(65, 65, 65))
                    .add(jLoginPanelLayout.createSequentialGroup()
                        .add(jLoginPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jPasswordField)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, nameField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        jLoginPanelLayout.setVerticalGroup(
            jLoginPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLoginPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(nameLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(nameField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(36, 36, 36)
                .add(jLabel2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPasswordField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jLoginPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(infoLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE)
                    .add(loginButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 39, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jScrollPane1.setBorder(null);
        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setOpaque(false);

        jUserPanel.setBackground(resourceMap.getColor("jUserPanel.background")); // NOI18N
        jUserPanel.setName("jUserPanel"); // NOI18N
        jUserPanel.setOpaque(false);
        jUserPanel.setPreferredSize(new java.awt.Dimension(550, 515));
        jScrollPane1.setViewportView(jUserPanel);

        AddUserButton.setFont(resourceMap.getFont("AddUserButton.font")); // NOI18N
        AddUserButton.setText(resourceMap.getString("AddUserButton.text")); // NOI18N
        AddUserButton.setName("AddUserButton"); // NOI18N
        AddUserButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddUserButtonActionPerformed(evt);
            }
        });

        addTeacherButton.setFont(resourceMap.getFont("addTeacherButton.font")); // NOI18N
        addTeacherButton.setText(resourceMap.getString("addTeacherButton.text")); // NOI18N
        addTeacherButton.setName("addTeacherButton"); // NOI18N
        addTeacherButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTeacherButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 550, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(AddUserButton, 0, 0, Short.MAX_VALUE)
                    .add(addTeacherButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLoginPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(44, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 515, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(layout.createSequentialGroup()
                        .add(jLoginPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(AddUserButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 76, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addTeacherButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(15, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginButtonActionPerformed
        Pluggis g = Pluggis.getInstance();

        if (jPasswordField.getText().equals(g.getSelectedUser().getPassword())) {

            g.getMainView().removeAll();

            if (g.getSelectedUser().getStatus() == true) {

                //g.getMainView().add(new TeacherView());
                g.getMainView().add(new LoggedInTeacherView());
                g.getMainView().repaint();
                g.getMainView().validate();
            } else {


                g.getMainView().add(new LoggedInView());
                g.getMainView().repaint();
                g.getMainView().validate();
            }
        } else {
            infoLabel.setText("Fel Lösenord!");
        }



        // TODO add your handling code here:
    }//GEN-LAST:event_loginButtonActionPerformed

    private void AddUserButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddUserButtonActionPerformed

        new AddUserWindow().setVisible(true);


    }//GEN-LAST:event_AddUserButtonActionPerformed

    private void addTeacherButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTeacherButtonActionPerformed
        new AddTeacherWindow().setVisible(true);    }//GEN-LAST:event_addTeacherButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddUserButton;
    private javax.swing.JButton addTeacherButton;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jLoginPanel;
    private javax.swing.JPasswordField jPasswordField;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jUserPanel;
    private javax.swing.JButton loginButton;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent pce) {
        String change = pce.getPropertyName();

        if (change.equals("GUI.RepaintView")) {
            this.revalidate();
            this.repaint();
            jScrollPane1.setSize(jUserPanel.getX(), jUserPanel.getY());
            jScrollPane1.revalidate();
            jScrollPane1.repaint();

        }

        if (change == "GUI.SelectedUserChanged") {
            User u = (User) pce.getNewValue();
            nameField.setText(u.getName());

        }
    }
}
